// order detail
// ============

define(['zepto', 'utils', 'component/countdown'], function($, utils){
  return {
    init: function(data) {
        this.initModal(data.pickupLocationId, data.returnLocationId)
    	this.cancelOrderBooking(data.orderId, data.agentMobile);
    	this.cancelOrder(data.orderId, data.agentMobile);
    	this.downloadPdf();
    },
    initModal: function(pickupLocationId, returnLocationId) {
        var that = this;
        $.ajax({
          type: "GET",
          url: window.__CTX__.root + "/locations/"+pickupLocationId+"/working-hours",
          dataType: "json",
          success: function(data) {
            if(data.code == 0) {
               console.log("s",data.data)
                var pickupWorkHtml = ""
                $.each(data.data.workingHours, function(index, item){
                  pickupWorkHtml+= '<p style="margin: .25rem 0;"><span style="font-weight: bold;">'+ index +'</span>： ' + item + '</p>'
                });
                that.pickupWorkHtml = pickupWorkHtml
            } else {
              $.toast(data.message);
            }
          },
          error: function() {
            $.toast("查询失败");
          }
        })
        
        $.ajax({
          type: "GET",
          url: window.__CTX__.root + "/locations/"+returnLocationId+"/working-hours",
          dataType: "json",
          success: function(data) {
            if(data.code == 0) {
            	console.log("d",data.data)
                var returnLocationId = ""
                $.each(data.data.workingHours, function(index, item){
                	returnLocationId+= '<p style="margin: .25rem 0;"><span style="font-weight: bold;">'+ index +'</span>： ' + item + '</p>'
                });
                
                that.returnLocationId = returnLocationId;
            } else {
              $.toast(data.message);
            }
          },
          error: function() {
            $.toast("查询失败");
          }
        })
        
    	$(document).on('click','.icon-layout .iconfont', function (e) {
    	    $.modal({
              title:  '营业时间',
              text: '<div class="business-hours">'+
    	      ($(e.currentTarget).data("work") == "pickupLocation" ? that.pickupWorkHtml : that.returnLocationId)
      	            +'</div>',
              buttons: [
                {
                  text: '<span style="color: #5d5c5c;">关闭</span>',
                },
              ],
              extraClass: 'business-hours-modal'
            })
    	  });
          
          $(document).on('click', '.modal-overlay', function() {
            $.closeModal('.modal');
          })
    },
    
    cancelOrderBooking: function(orderId, agentMobile) {
        var currentThat = this;
    	$(document).on('click', '.cancelBooking', function(e){
    		e.preventDefault()
    		var that = this
    		$.confirm('确认取消预定?', function () {
    			var defer = $.Deferred()
                $.showIndicator()
                $.post($(that).data('href'), {"_method" : "PATCH"})
                  .done(function(response) {
                    $.hideIndicator()
                    utils.processResult(response, function(result) {
                    	$.toast(result.message || '取消预定成功')
                    	currentThat.requestOrderCancelMessage(agentMobile, orderId, function(data){},function(){});
                    	setTimeout(function() {
                            window.location.href = window.__CTX__.root + "/orders"; 
                       }, 2000)
                    }, function(result) {
                      $.toast(result.message || '取消预定失败')
                    })
                  })
                return defer.promise();
    		});
    		
    	})
    	
    },
    
    cancelOrder: function(orderId, agentMobile) {
        var currentThat = this;
    	$(document).on('click', '.cancelOrder', function(e){
    		var that = this
    		e.preventDefault()
    		$.confirm('确认取消订单?', function () {
    			var defer = $.Deferred()
                $.showIndicator()
                $.post($(that).data('href'), {"_method" : "PATCH"})
                  .done(function(response) {
                    $.hideIndicator()
                    utils.processResult(response, function(result) {
                    	$.toast(result.message || '取消订单成功')
                    	currentThat.requestOrderCancelMessage(agentMobile, orderId, function(data){},function(){});
                    	setTimeout(function() {
                            window.location.href = window.__CTX__.root + "/orders"; 
                       }, 2000)
                    }, function(result) {
                      $.toast(result.message || '取消订单失败')
                    })
                  })
                return defer.promise();
    		});
    		
    	})
    	
    },
    
    /**
     * Request server to send order cancel message to mobile
     */
    requestOrderCancelMessage: function(mobile, orderId, success, failure) {
      $.showIndicator()
      utils.requestCancelOrderMessage(mobile, orderId, function(result) {
        $.hideIndicator()
        success && success(result.data)
      }, failure)
    },
    
    downloadPdf: function() {
      $(document).on('click','#pdfUrl', function(e) {
        if (($.device.android || $.device.androidChrome) && $.device.isWeixin) {
          $.popup('.popup-out');
          return;
        }
        window.location.href = $(this).data('pdf-url');
      })
    }
  }

})